<template>
  <div>
    <div class="title">
      <span><i>1</i>输入找回账号</span>
      <span><i>2</i>回答密保问题</span>
      <span class="active"><i>3</i>重置密码</span>
    </div>
    <div class="box">
      <div class="input-item">
        <label>请输入新密码：</label>
        <input type="text" v-model.trim="password" />
      </div>
      <button @click="change">上一步</button>
      <button @click="reset">立即重置</button>
    </div>
  </div>
</template>

<script>
import axios from "axios";
axios.defaults.baseURL = "http://124.223.14.236:3001/api";
export default {
  props: {
    answer: {
      type: String,
      require: true,
    },
  },
  data() {
    return {
      password: "",
      id: "",
      num: "",
    };
  },
  created() {
    this.num = JSON.parse(localStorage.getItem("id"));
    this.id = String(this.num);
  },
  methods: {
    //上一步
    change() {
      this.$emit("change", "step2");
    },
    //修改密码
    async reset() {
      if (this.password.trim().length === 0) return alert("密码不能为空");
      const { data } = await axios.post("/user/resetPwd", {
        id: this.id,
        password: this.password,
        answer: this.answer,
      });
      if (!data.success) {
        alert("密保问题回答错误");
        this.change();
      } else {
        alert("密码修改成功");
        this.answer = "";
      }
    },
  },
};
</script>

<style scoped>
@import url("@/assets/css/index.css");
</style>
